#ifndef FPARTICLE_SYSTEM
#define FPARTICLE_SYSTEM

//#include "btBulletDynamicsCommon.h"
#include "Magnet.h"
#include "Vector3.h"
#include <vector>
#include <map>
#include <math.h>
#include <algorithm>

class FParticleSystem {

private:

	int numParticles;
	
	Vector3 SystemPos, domain;

	//fparticle attribute///

	Vector3 *pos;		//position
	float *d;			//density(=amount of particle)
	float max_d;		//max density

	float m;			//mass 

	Vector3 *mm;		//magnetic moment
	Vector3 *vel;		//velocity

	int *connect;		//connection index with other fparticles (-x,-y,-z,x,y,z)
						//if this value sets -1, there is no connection

	//////////////////////


	Vector3 numGrid;		//number of grid at each axis (x,y,z)
	Vector3 gap;

	float timeStepSize, dt;
	float maxSubTimeStepSize;


	Vector3 gravity;

public:
	
	FParticleSystem(int _numParticles = 500) : maxSubTimeStepSize(0.0001),
	gravity(0, -9.8, 0), SystemPos(0, 0, 0), domain(5, 5, 5)
	{
		numParticles = _numParticles;
		
		int ngrid = pow(numParticles, 0.5);
		numGrid[0] = numGrid[2] = ngrid;
		numGrid[1] = 1;

		numParticles = numGrid[0] * numGrid[1] * numGrid[2];

		gap = domain / numGrid;
		

		pos = new Vector3[numParticles];
		d = new float[numParticles];
		mm = new Vector3[numParticles];
		vel = new Vector3[numParticles];
		connect = new int[numParticles * 6];

		ParticleSeeding();

	};

	~FParticleSystem() {
		delete [] pos;
		delete [] d;
		delete [] mm;
		delete [] vel;
		delete [] connect;
	};

	void ParticleSeeding();

	void Simulation();
	void Simulation(const float timeStep) { timeStepSize = timeStep; Simulation(); };

	void Advection();



	void Rendering_Grid();
};

#endif